//
//  MBSpaceNewsViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceNewsViewController.h"
#import "MBSpaceSystenNewsViewController.h"
#import "MBSpaceNewsDetailViewController.h"
#import "MBLoginController.h"

#import "MBSpaceNewsCell.h"
#import "MBSpaceSystemNewsCell.h"
#import "MBSpaceCollectionCompileView.h"
#import "EmojiView.h"

@interface MBSpaceNewsViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceNewsCellDelegate,UITextFieldDelegate,MBSpaceCollectionCompileViewDelegate,MBSpaceSystemNewsCellDelegate,EmojiViewDelegate> {
    UITableView                 *_tableView;
    NSMutableArray              *_mutArr;
    
    UIView                      *_inputView;
    UITextField                 *_inputField;
    UIButton                    *sureBt;
    UIView                      *_showderView;
    float                       inputTime;
    NSMutableDictionary         *_selectDataDic;
    MBSpaceNewsCell             *_selectCell;
    int                         page;
    NSInteger                   selectIndex;//测试
    
    MBSpaceCollectionCompileView *_compileView;
    BOOL                        _isCompiling; //是否编辑
    BOOL                        _isAllSelcting;//是否全选
    NSMutableArray              *_selectNewsArr;
    
    UIButton                    *emojiBt;
    EmojiView                   *_emojiView;
}

@end

@implementation MBSpaceNewsViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self registerForKeyboardNotifications];
    [self onLoadNew];
    if (ISIPAD) {
        [self onUpdateUIForView:self.orientation];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeForkeyboardNotifications];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _mutArr = [[NSMutableArray alloc]init];//WithObjects:@"0",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"0", nil
    selectIndex = -1;
    page = 1;
    _selectDataDic = [[NSMutableDictionary alloc]init];
    _isCompiling = NO;
    _isAllSelcting = NO;
    _selectNewsArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    [self setTitleView:@"消息盒子"];
   
    [self setLeftNav:YES];
//    [self setRightPlayerItem];
    [self setRightNav];
    
    [self onCreateUI];

}

#pragma mark - Actions
#pragma mark - UI
- (void)setLeftNav:(BOOL)isBack
{
    if (isBack) {
         [self setBackItem];
    } else {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 60, 30);
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14.]];
        [btn setTitle:@"全选" forState:UIControlStateNormal];
        [btn setTitle:@"全选" forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
        [btn addTarget:self action:@selector(onSelectAllMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        [self setLeftBarItem:btn];
    }
}

- (void)setRightNav
{
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setTitle:@"编辑" forState:UIControlStateNormal];
    [bt setTitle:@"编辑" forState:UIControlStateHighlighted];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    bt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [bt addTarget:self action:@selector(onSelectItemForDelete) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
}

- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
    [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    
    //键盘
    _inputView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40)];
    _inputView.backgroundColor = UIColorFromHex(0xf5f5f5);
    _inputView.hidden = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:_inputView];
    
    emojiBt = [UIButton buttonWithType:UIButtonTypeCustom];
    emojiBt.backgroundColor = [UIColor clearColor];
    emojiBt.frame = CGRectMake(0, 0, 50, _inputView.height);
    [emojiBt setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
    [emojiBt setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
    [emojiBt setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    [emojiBt addTarget:self action:@selector(onSelectEmojiAction:) forControlEvents:UIControlEventTouchUpInside];
    [_inputView addSubview:emojiBt];
    
    _inputField = [[UITextField alloc]initWithFrame:CGRectMake(50, 5, _inputView.width-50-50, 30)];
    _inputField.backgroundColor = [UIColor whiteColor];
    _inputField.placeholder = @"填写评论";
    _inputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputField.delegate = self;
    [_inputField setValue:[UIFont systemFontOfSize:13]forKeyPath:@"_placeholderLabel.font"];
    [_inputField resignFirstResponder];
    [_inputView addSubview:_inputField];
    
    sureBt = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBt.backgroundColor = [UIColor clearColor];
    sureBt.frame = CGRectMake(_inputView.width - 50, 0, 50, _inputView.height);
    [sureBt setTitle:@"回复" forState:UIControlStateNormal];
    [sureBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sureBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [sureBt addTarget:self action:@selector(onSelectSureAction) forControlEvents:UIControlEventTouchUpInside];
    [_inputView addSubview:sureBt];
    
    _showderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 216 - 40)];
    _showderView.backgroundColor = [UIColorFromHex(0x000000) colorWithAlphaComponent:.2];
    _showderView.hidden = YES;
    _showderView.userInteractionEnabled = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:_showderView];
    
    //表情
    _emojiView = [[EmojiView alloc]init];
    _emojiView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:_emojiView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapHideInputAction)];
    [_showderView addGestureRecognizer:tap];
    
    //批量弹出框
    _compileView = [[MBSpaceCollectionCompileView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100*PERCEN) WithIndex:2];
    _compileView.delegate = self;
    _compileView.orientation = self.orientation;
    _compileView.hidden = YES;
    [self.view addSubview:_compileView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _inputView.frame = CGRectMake(0, (_inputView.isHidden?_tableView.height:_inputView.top), SCREEN_WIDTH, 40);
    _inputField.frame = CGRectMake(10, 5, _inputView.width-60, 30);
    sureBt.frame = CGRectMake(_inputView.width - 50, 0, 50, _inputView.height);
    _showderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _inputView.top);
    [_tableView reloadData];
}

#pragma mark - 加载数据
- (void)onLoadNew
{
    page = 1;
    [self onLoadData];
}

- (void)onLoadMore
{
    ++page;
    [self onLoadData];
}


- (void)onLoadData
{
    UserModel *model = [UserManager standerDefult].userModel;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_MESSAGES_LIST_URL_PATH,model.UserID,[NSNumber numberWithInt:page]] view:nil success:^(NSDictionary *json) {
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if ([arr isKindOfClass:[NSArray class]]) {
                    if (page == 1) {
                        [_mutArr removeAllObjects];
                    }
                    
                    //ShowTitle ForumTitle
                    for (NSDictionary *dic in arr) {
                        if ([dic.allKeys containsObject:@"MesType"]) {
                            if ([[dic objectForKey:@"MesType"]intValue] == 0) {
                                //系统
                                [_mutArr addObject:dic];
                            }
                        }
                        if ([dic.allKeys containsObject:@"ShowTitle"]) {
                            if (![[dic objectForKey:@"ShowTitle"]isKindOfClass:[NSNull class]]) {
                                [_mutArr addObject:dic];
                            }
                        }
                        else if ([dic.allKeys containsObject:@"ForumTitle"]) {
                            if (![[dic objectForKey:@"ForumTitle"]isKindOfClass:[NSNull class]]) {
                                [_mutArr addObject:dic];
                            }
                        }
                    }
//                    [_mutArr addObjectsFromArray:arr];
                }
                [_tableView reloadData];
            } else {
//                [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
//                [_mutArr removeAllObjects];
                [_tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
}

#pragma mark - 方法
//进入播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

#pragma mark - 编辑
- (void)onSelectItemForDelete
{
    //批量
    _isCompiling = !_isCompiling;
    if (_isCompiling) {
        //弹出批量框
        _tableView.height = SCREEN_HEIGHT - 100*PERCEN;
        _compileView.top = _tableView.bottom;
        _compileView.hidden = NO;
        
        [self setLeftNav:NO];
    } else {
        //隐藏
        _tableView.height = self.view.height;
        _compileView.top = _tableView.bottom;
        _compileView.hidden = YES;
        _isAllSelcting = NO; //不全选
        
        [self setLeftNav:YES];
    }
    [_tableView reloadData];
}

- (void)onSelectItemForCancel:(MBSpaceCollectionCompileView *)view
{
    [self onSelectItemForDelete];
}

#pragma mark - 删除
- (void)onSelectItemForDelete:(MBSpaceCollectionCompileView *)view
{
    NSMutableString *mutArr = [[NSMutableString alloc]init];
    int i = 0;
    for (NSDictionary *dic in _selectNewsArr) {
        NSString *idStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ID"]];
        [mutArr appendString:idStr];
        
        if (i < _selectNewsArr.count -1) {
            [mutArr appendString:@","];
        }
        
        ++i;
    }
    
    UserModel *model = [UserManager standerDefult].userModel;
    [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_DELETE_MESSAGE_URL_PATH,model.UserID,mutArr] view:nil success:^(id json) {
        
        [MBProgressHUD hideHUDForView:_tableView animated:YES];
        if ([json[@"ret_code"]intValue] == 0) {
            for (NSDictionary *dic in _selectNewsArr) {
                NSUInteger index = [_mutArr indexOfObject:dic];
                [_mutArr removeObject:dic];
                [_tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            }
            
            [_selectNewsArr removeAllObjects];
            
//            if (_mutArr.count == 0) {
                //已下载歌曲删除完
                [self onSelectItemForDelete];
//            }
            
        }
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:_tableView animated:YES];
    }];
    
    
}

#pragma mark - 全选和全不选
- (void)onSelectAllMusicAction:(UIButton *)bt
{
    bt.selected = !bt.selected;
    if (bt.selected) {
        [bt setTitle:@"全不选" forState:UIControlStateNormal];
        [bt setTitle:@"全不选" forState:UIControlStateHighlighted];
        [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 15)];
    } else {
        [bt setTitle:@"全选" forState:UIControlStateNormal];
        [bt setTitle:@"全选" forState:UIControlStateHighlighted];
        [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
    }
    
    _isAllSelcting = bt.selected;
    
    //测试
    [_selectNewsArr removeAllObjects];
    [_selectNewsArr addObjectsFromArray:_mutArr];
    
    //删除、收藏是否可选中
    [_compileView onUpdateDataForDelegateAndCollection:(_selectNewsArr.count > 0 ? YES : NO)];
    
    [_tableView reloadData];
}

#pragma mark - 键盘相关
- (void)registerForKeyboardNotifications
{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShow:)
     
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
}

- (void)removeForkeyboardNotifications
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    float keyboardhight = 0;
    if(kbSize.height == 216)
    {
        keyboardhight = 0;
    }
    else
    {
        keyboardhight = kbSize.height-216;   //258 - 216 系统键盘的两个不同高度
    }
    //输入框位置动画加载
    inputTime = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    _emojiView.alpha = 0;
    emojiBt.selected = NO;
    [self onAnimationWithInputView:NO WithHeight:keyboardhight WithDuration:inputTime];
}

//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    //do something
    [self onAnimationWithInputView:YES WithHeight:0 WithDuration:inputTime];
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    [self onAnimationWithInputView:YES WithHeight:0 WithDuration:inputTime];
    
    //释放
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

//输入框和遮挡层动画效果
- (void)onAnimationWithInputView:(BOOL)isHidden WithHeight:(float)heght WithDuration:(float)time
{
    [UIView animateWithDuration:time animations:^{
        if (_emojiView.alpha == 0) {
            emojiBt.selected = NO;
            _emojiView.top = SCREEN_WIDTH;
            _inputView.top = (isHidden == YES ? SCREEN_HEIGHT : SCREEN_HEIGHT-216-heght-40);
            _inputView.hidden = isHidden;
            _showderView.hidden = isHidden;
            _showderView.height = _inputView.top;
        } else {
            emojiBt.selected = YES;
            _emojiView.top = SCREEN_HEIGHT - 216;
            _inputView.top = _emojiView.top-40;
            _inputView.hidden = NO;
            _showderView.height = _inputView.top;
            _showderView.hidden = NO;
        }
    } completion:^(BOOL finished) {
        if (finished) {
           // _inputField.text = @"";
        }
    }];
}

- (void)onTapHideInputAction
{
    [_inputField resignFirstResponder];
    _emojiView.alpha = 0;
    emojiBt.selected = NO;
    [self onAnimationWithInputView:YES WithHeight:0 WithDuration:inputTime];
}

#pragma mark -表情
- (void)onSelectEmojiAction:(UIButton *)bt
{
    if (bt.selected == NO) {
        _emojiView.alpha = 1.f;
        [_inputField resignFirstResponder];
    } else {
        _emojiView.alpha = 0.f;
        [_inputField becomeFirstResponder];
    }
    
    //    bt.selected = !bt.selected;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int lengthCount = (int)(textField.text.length + string.length);
    if (lengthCount >= 101) {
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 回复
- (void)onSelectControlForReplyWithValue:(NSDictionary *)value withView:(MBSpaceNewsCell *)view
{
    if ([value isKindOfClass:[NSDictionary class]]) {
        [_selectDataDic removeAllObjects];
        [_selectDataDic addEntriesFromDictionary:value];
        
        
        //响应键盘
        if (_inputField) {
            _emojiView.alpha = 0;
            emojiBt.selected = NO;
            [_inputField becomeFirstResponder];
            _selectCell = view;
        }
    }
    
}

//发送回复信息
- (void)onSelectSureAction
{
    [self onTapHideInputAction];
    
    if (!_selectDataDic) return;
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model.UserID) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    //测试
    NSString *text = _inputField.text;
    NSString *uniStr = [NSString stringWithUTF8String:[text UTF8String]];
    NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString *goodStr = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding] ;
    

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *url;
    if ([_selectDataDic.allKeys containsObject:@"CommentType"]) {
        if (![[_selectDataDic objectForKey:@"CommentType"] isKindOfClass:[NSNull class]]) {
            if ([[_selectDataDic objectForKey:@"CommentType"] intValue] == 1) {
                //亲子秀
                url = [NSString stringWithFormat:FORUM_REPLY_INFO_URL_PATH,model.UserID,[_selectDataDic objectForKey:@"CommentID"],[_selectDataDic objectForKey:@"UserID"],goodStr];
            }
            else if ([[_selectDataDic objectForKey:@"CommentType"] intValue] == 2) {
                //帖子
                url = [NSString stringWithFormat:FORUM_REPLY_INFO_URL_PATH,model.UserID,[_selectDataDic objectForKey:@"CommentID"],[_selectDataDic objectForKey:@"UserID"],goodStr];
            }
        }
    }
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
                _inputField.text = @"";
//            NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]initWithDictionary:_selectDataDic];
//            [mutDic setValue:text forKey:@"ReplyCont"];
//            int indexDic = (int)[_mutArr indexOfObject:_selectDataDic];
//            if (_mutArr.count > indexDic) {
//                [_mutArr replaceObjectAtIndex:indexDic withObject:mutDic];
//                [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexDic inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
//            }
        }
        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - 勾选
- (void)onSelectControlForGouXuanWithValue:(id)value WithSelect:(BOOL)isSelect WithView:(MBSpaceNewsCell *)view
{
    if (isSelect) {
        //保存
        [_selectNewsArr addObject:value];
    } else {
        //删除
        if ([_selectNewsArr containsObject:value]) {
            [_selectNewsArr removeObject:value];
        }
    }
    
    //删除、收藏是否可选中
    [_compileView onUpdateDataForDelegateAndCollection:(_selectNewsArr.count > 0 ? YES : NO)];
    
    if (_selectNewsArr.count != _mutArr.count) {
        //不是全选 所有显示全选
        [self setLeftNav:NO];
    }
}

//系统
- (void)onSelectCtrlForGouXuanWithValue:(id)value WithSelect:(BOOL)isSelect WithView:(MBSpaceSystemNewsCell *)view
{
    if (isSelect) {
        //保存
        [_selectNewsArr addObject:value];
    } else {
        //删除
        if ([_selectNewsArr containsObject:value]) {
            [_selectNewsArr removeObject:value];
        }
    }
    
    //删除、收藏是否可选中
    [_compileView onUpdateDataForDelegateAndCollection:(_selectNewsArr.count > 0 ? YES : NO)];
    
    if (_selectNewsArr.count != _mutArr.count) {
        //不是全选 所有显示全选
        [self setLeftNav:NO];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_mutArr.count > indexPath.row) {
        NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
        if ([dic.allKeys containsObject:@"MesType"]) {
            if ([[dic objectForKey:@"MesType"] intValue] == 0) {
                //系统
                static NSString *cellID = @"News_system_Cell";
                MBSpaceSystemNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
                if (!cell) {
                    cell = [[MBSpaceSystemNewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.delegate = self;
                }
                cell.orientation = self.orientation;
                [cell onCompileCellForLayout:_isCompiling WithAllSelct:_isAllSelcting];
                if (_mutArr.count > indexPath.row) {
                    
                    cell.dataDic = dic;
                }
                return cell;
            } else {
                //网友评论
                static NSString *cellID = @"News_Cell";
                MBSpaceNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
                if (!cell) {
                    cell = [[MBSpaceNewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.delegate = self;
                }
                cell.orientation = self.orientation;
                [cell onCompileCellForLayout:_isCompiling WithAllSelct:_isAllSelcting];
                if (_mutArr.count > indexPath.row) {
                    
                    cell.dataDic = dic;
                    
                    NSString *replay = [dic objectForKey:@"ReplyCont"];
                    if (replay) {
                        cell.isHide = NO;
                    } else {
                        cell.isHide = YES;
                    }
                }
                
                return cell;
            }
        }
    }

    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_mutArr.count > indexPath.row) {
        NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
        if ([dic.allKeys containsObject:@"MesType"]) {
            if ([[dic objectForKey:@"MesType"] intValue] == 0) {
                //系统反馈
                return 187*PERCEN;
            } else {
                //网友评论
                NSString *replay = [dic objectForKey:@"ReplyCont"];
                if (replay) {
                    return 256*PERCEN+20;
                }
                return 256*PERCEN;
            }
        }
    }
    
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_mutArr.count > indexPath.row) {
        if (_isCompiling == YES) {
            //处于编辑状态
            MBSpaceNewsCell *cell = (MBSpaceNewsCell *)[tableView cellForRowAtIndexPath:indexPath];
            if ([cell isKindOfClass:[MBSpaceNewsCell class]]) {
                [cell onGouXuanToSelectAction:cell.selectBt];
            }
            else if ([cell isKindOfClass:[MBSpaceSystemNewsCell class]]) {
                [cell onGouXuanToSelectAction:cell.selectBt];
            }
        } else {
            NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
            if ([dic.allKeys containsObject:@"MesType"]) {
                if ([[dic objectForKey:@"MesType"] intValue] == 0) {
                    //系统消息
                    MBSpaceSystenNewsViewController *play = [[MBSpaceSystenNewsViewController alloc]init];
                    play.dataDic = dic;
                    [self.navigationController pushViewController:play animated:YES];
                } else {
                    //网友评论
                    MBSpaceNewsDetailViewController *news = [[MBSpaceNewsDetailViewController alloc]init];
                    news.dataDic = dic;
                    news.isFormMyNews = YES;
                    [self.navigationController pushViewController:news animated:YES];
                }
            }
        }
    }

}

#pragma mark - Emoji
- (void)onSelectItemForEmojiView:(NSString *)emoji withView:(EmojiView *)view
{
    if ([emoji isKindOfClass:[NSString class]]) {
        if (emoji.length > 0) {
            _inputField.text = [NSString stringWithFormat:@"%@%@",_inputField.text,emoji];
        }
    }
}


@end
